package midas.mw.crt.presence.util;

import java.security.MessageDigest;

import midas.mw.cfg.CfgError;
import midas.mw.cfg.CfgLogging;
import midas.mw.cfg.log.Logger;
import net.jxta.id.IDFactory;
import net.jxta.peergroup.PeerGroupID;
import net.jxta.pipe.PipeID;

public class MD5ID {
    
    public static final String functionSeperator = "~";
    
    /**
     * Create a PipeID based on the BinaryID type with a digest of the clearTextID and function.
     *
     * @param peerGroupID Parent peer group ID.
     * @param clearTextID String used as the significant part of the address
     * @param function String used to differentiate different clearTextID addresses
     * 					(can be null).
     * @return PipeBinaryID with the digest hash of the string: clearTextID + "~" +
     *  		function.
     */
    public static final PipeID createPipeID(PeerGroupID peerGroupID,String clearTextID, String function)
    {
    	CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "MD5ID.createPipeID(PeerGroupID, " + clearTextID + ", " + function + ")");
    	        
        byte[] digest = generateHash(clearTextID, function);
        //return new PipeID( /*(net.jxta.impl.id.UUID.PeerGroupID)*/peerGroupID, digest );
        return (PipeID)IDFactory.newPipeID(peerGroupID, digest );
    }
    
      
    
    /**
     * Generates an SHA-1 digest hash of the string: clearTextID + "-" + function
     * or: clearTextID if function was blank.
     * <p>
     * Note that the SHA-1 used only creates a 20 byte hash
     *
     * @param clearTextID A string that is to be hashed. This can be any string 
     * 						used for hashing or hiding data.
     * @param function A function related to the clearTextID string. This is used
     * 			 			to create a hash associated with clearTextID so that
     * 						it is a unique code.
     * @return Array of bytes containing the hash of the string: clearTextID + 
     * 						"-" + function or clearTextID if function was blank.
     * 						Can return null if SHA-1 does not exist on platform.
     */
    private static final byte[] generateHash(String clearTextID, String function) 
    {
    	CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "MD5ID.generateHash(" + clearTextID + ", " + function + ")");
    	
        String id;
        
        if (function == null)
            id = clearTextID;
        else
            id = clearTextID + functionSeperator + function;
        
        byte[] buffer = id.getBytes();
        
        MessageDigest algorithm = null;
        
        try {
            algorithm = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
        	CfgError.errorReporting(CfgError.ERR_OPER, Logger.COMPONENT_CRT, CfgError.SEV_FATAL, 
        			"MD5ID.generateHash(" + clearTextID + ", " + function + ") " +
        			"Cannot load selected Digest Hash implementation: '" + e.getMessage() + "'", null);
            return null;
        }
        
        
        // Generate the digest.
        algorithm.reset();
        algorithm.update(buffer);
        
        try{
            byte[] digest1 = algorithm.digest();
            return digest1;
        }catch(Exception de){
        	CfgError.errorReporting(CfgError.ERR_OPER, Logger.COMPONENT_CRT, CfgError.SEV_FATAL, 
        			"MD5ID.generateHash(" + clearTextID + ", " + function + ") " +
        			"Failed to creat a digest: '" + de.getMessage() + "'", null);            
            return null;
        }
    }
}
